<style scoped>
    .company >>> .layui-form-select {
        z-index: auto;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加产品价格</legend>
        </fieldset>

        <form class="layui-form " >
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">品牌</label>
                <div class="layui-input-block">
                    <select type="text" id="brandid" lay-filter="brandid" autocomplete="off"
                            class="layui-select" lay-search>
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">品种</label>
                <div class="layui-input-block">
                    <select  type="text" name="productguid"    id="productguid" lay-filter="productguid" autocomplete="off"   lay-verify="required" class="layui-select" lay-search>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">产品</label>
                <div class="layui-input-block">
                    <select type="text" name="salesspecificationid"    id="salesspecificationid" lay-filter="salesspecificationid" autocomplete="off"   lay-verify="required" class="layui-select" lay-search>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">销售价格</label>
                <div class="layui-input-block">
                    <input type="text" name="price" required lay-verify="required"  placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">工厂价格</label>
                <div class="layui-input-block">
                    <input type="text" name="factoryprice" required lay-verify="required"  placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">币种</label>
                <div class="layui-input-block">
                    <select type="text" name="currency" id="currency" lay-filter="currency"  autocomplete="off" lay-verify="required" class="layui-select" >
                        <option v-for="(currency, index) in currencys" :value="currency.dictkey">{{currency.dicttext}}</option>
                    </select>
                </div>
            </div>
            <!--<div class="layui-form-item">
                <label class="layui-form-label">计价方式</label>
                <div class="layui-input-block" >
                    <select type="text" name="valuation" id="valuation" lay-filter="valuation"  autocomplete="off" lay-verify="required" class="layui-select">
                        <option v-for="(valuation, index) in valuations" :value="valuation.dictkey">{{valuation.dicttext}}</option>
                    </select>
                </div>
            </div>-->
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        <div style="height: 200px"></div>



        </form>
    </div>
</template>

<script>
    let qs = require('qs');
    export default {
        name: 'addproductprice',
        data () {
            return {
                productTypes: [],
                valuations:[ {dicttext:"按件",dictkey:1},{dicttext:"按吨",dictkey:2},],
                currencys:[ {dicttext:"人民币",dictkey:1},{dicttext:"美元",dictkey:2},],
                specs: [],
                productid: '',
                brandid: ''
            }
        },
        mounted() {
            let that = this;
            this.getProductTypes();
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;
                form.render();
                that.loadBrands();
                that.loadProducts();
                that.loadSalesspecification()
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    $.ajax({
                        url: '/sales/Productprice!update.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.code == 1) {
                                that.toast("添加成功");
                                that.$parent.switchPage('productprice');
                            } else {
                                that.toast(res.msg);
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });
            layui.use(['form'], function () {
                var form = layui.form;
                form.on('select(productguid)', function(data){
                    that.loadSalesspecification(data.value)
                    form.render('select');//select是固定写法 不是选择器
                });
                form.on('select(brandid)', function(data){
                    that.brandid = data.value;
                    that.filterPro();
                });
            });
        },
        updated() {
            layui.form.render('select');
            layui.form.render();
        },
        methods: {
            //获取品种
            loadProducts:  function (){
                $('#productguid').append(new Option('请选择', ""));// 下拉菜单里添加元素。
                $.ajax({
                    url: '/sales/Productprice!getBaseInfo.action',
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != 'error')  {
                            $.each(res.info, function (index, item) {
                                $('#productguid').append(new Option(item.productname , item.guid));
                            });
                            layui.form.render("select");
                        } else {
                            alert("系统错误");
                        }
                    }
                });
            },
            //获取规格
            loadSalesspecification:  function (val = ''){
                let that = this;

                $.ajax({
                    url: '/sales/Salesspecification!getBase.action',
                    data: {productGuid:val},
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != 'error')  {
                            that.specs = res.info;
                            that.filterPro();
                        } else {
                            alert("系统错误");
                        }
                    }
                });
            },
            filterPro: function() {
                let that = this;
                let products = this.specs.filter(item => {
                    if (that.brandid != '') {
                        return item.brandid == that.brandid;
                    } else {
                        return true;
                    }
                });
                $("#salesspecificationid").empty();
                $('#salesspecificationid').append(new Option('请选择', ""));// 下拉菜单里添加元素。
                $.each(products, function (index, item) {
                    $('#salesspecificationid').append(new Option(item.name , item.guid));
                });
                layui.form.render("select");
            },
            getProductTypes: function() {
                this.$post('/sales/Sellform!findDict.action', qs.stringify({type: 91})).then(response => {
                    if (response.statusText == "OK") {
                        this.productTypes = response.data;
                    } else {
                        this.toastText('获取产品类型数据失败');
                    }
                });
            },
            //获取品牌
            loadBrands:  function (){
                $.ajax({
                    url: '/sales/Salesspecification!getBaseInfo.action',
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.msg == "scucess") {
                            $.each(res.brands, function (index, item) {
                                $('#brandid').append(new Option(item.name, item.guid));
                            });
                            layui.form.render("select");
                        } else {
                            alert("系统错误");
                        }
                    }
                });
            },

        }
    }
</script>
